* This program enables control of the other job accessibility programs;
* Run the program by entering nohup sas control_jobaccess.sas;
* To activate a specific program, mark it as 1, rather than 0;
* Programs are listed sequentially, so they could all be run at once;
* by Mark Kutzbach (kutzb001) for Spatial Mismatch project;

* options;
* options obs=10000;
* options spool;
options mprint mlogic symbolgen;

* Vintage of job accessibility;
%let vintage = control_11;

* Run these before processing mass displacement sample;
%let run0=1; * retain any intermediate output files;
%let run0cpr=0; * extract relevant residence info from Composite Person Record (CPR);
%let run0cen=0; * extract relevant fields from 2000 Census short form (HCEF);
%let run0tract=0; * construct variables from public-use 2000 Census long form (SF3);
    * must set to on if using run0tract;
    %let run_census2000=1;
%let run0access=0; * Geography, measure access for home tracts (see Geography options below);
%let run0access2=0; * Compare travel time data, see below for options (outputs Table C1);
%let run0access3=0; * Census 2000 SF3 access, for measure in Table D2, Model 4;
%let run0access4=0; * Census 2000 SF3 access, summary;
%let run0modechoice=0; * Mode choice of auto and transit (outputs Tables B1, B2, B3);
    * estimate mode choice;
    %let run_mode_setup=1;
    %let run_mode_est=1;

* Run mass displacement sample;
* metro-by-metro;
%let run1=0; * Identify displaced workers, link to CPR, demog - by state;
%let run2=0; * Earnings history of worker and household - by state;
%let run3=0; * Identify mass displacement employers - by state;
* national (all metros);
%let run4=0; * Join workers to employer, earnings, tract, vars - go to national (Table );
%let run5=0; * Predict auto use and job accessibility;
%let run6=0; * Create additional estimation variables and merge;
%let run7=0; * Export to stata;
%let run8=1; * Disclosure analysis on summary stats file;

* Metro areas; 
* includes the counties with travel time data from MPO in same state as central city of MSA;
%let metord_start=1;
* let metrolist  = ind chi min buf pit mil det col cle;
%let metrolist  = ind chi min buf pit mil det col cle;
* list for mode choice estimation (must be there in 2000);
%let metrolistmode  = ind chi min buf pit mil;
%let stlist     = in  il  mn  ny  pa  wi  mi  oh  oh;
%let stcodelist = 18  17  27  36  42  55  26  39  39;
%let stcodelistq ='18','17','27','36','42','55','26','39','39';
%let countylist = '011','057','059','063','081','095','097','109','145' V
                  '031','043','089','097','111','197' V
                  '003','019','037','053','123','139','163' V
                  '029','063' V
                  '003','005','007','019','051','125','129' V
                  '079','089','101','131','133' V
                  '099','125','163' V
                  '041','049','089' V
                  '035','055','085','093','103'
                  ;
* QTIME for each Metro: LEHD count of quarters from 1985Q1;
* 2000:1 is 61, 1998:1 is 53, 2005:4 is 84;
%let sep_qtr_start_list  = 61  61  61  61  61  61  65  69  69;
%let sep_qtr_end_list    = 84  84  84  84  84  84  84  84  84;
%let sep_year_start_list = 2000 2000 2000 2000 2000 2000 2001 2002 2002;
%let sep_year_end_list   = 2005 2005 2005 2005 2005 2005 2005 2005 2005;
* state and metro;
%let stmetrolist= inind ilchi mnmin nybuf papit wimil midet ohcol ohcle;

/* directories (root paths converted to ...) */
%let dirprog = .../kutzb001/access;
%let dirdata = .../04/kutzb001/access;
%let dirtemp = /saswrk/users/kutzb001/access;
%let dirprogvint = &dirprog./vintages/&vintage.;
%let dirdatavint = &dirdata./vintages/&vintage.;
%let dirtempvint = &dirtemp./vintages/&vintage.;
%let dirdisc = &dirdatavint./disclosure;
%let dirsnap = .../snapshot/prod/s2008;
%let dircen  = .../master/censas;
%let dircpr = .../master/pcfcpr/sasdata/lehd1_2012_04_20;
%let dirotmext= .../master/otm/vintages/ext/us;
libname OUTDATA "&dirdatavint.";
libname SAVEDATA "&dirdatavint.";
libname DISC "&dirdisc.";
libname CENSHORT "&dircen./hcef2000/sasdata" access=readonly;
libname CENLONG "&dircen./scef2000/sasdata" access=readonly;
libname CPR "&dircpr." access=readonly;
libname GEO "&dirdata./input_data/geo" access=readonly;
libname OTM "&dirdata./input_data/attrib/evalk/area_profile" access=readonly;
libname CEN2000 "&dirdata./input_data/attrib/census2000_sf3" access=readonly;
libname OTMFLOW "&dirdata./input_data/attrib/otm" access=readonly;
libname TRACTCEN "&dirdata./input_data/attrib/census2000" access=readonly;
libname PROX "&dirdata./input_data/proximity" access=readonly;
libname OTMEXT "&dirotmext./20090720_110800" access=readonly;

* Parameter settings;
* quarters of job history before displacement quarter;
%let emphist_span=8;
* required minimum quarters of tenure before displacement;
%let emp_qtr_span=4;
* required minimum quarters of no earnings from job following separation;
%let sep_qtr_span=8;

* earnings levels for rehiring;
%let earn_lvl="00 50 75 90";
%let earn_mult="0 0.5 0.75 0.9";
%let agemin=20;
%let agemax=64;

* Geography options (see measure_access.sas);
* create geo pair frame and calculate distances;
%let run_geoframe_dist=1;
* merge proximity measures to use;
%let run_proxmeas=1; * run merge of proximity measures, used to calculate accessibility;
    %let use_prox_time_ctpp=0; * ctpp based and impute filled (may be obsolete);
    %let use_prox_time_mpo_auto=1; * mpo auto travel time;
    %let use_prox_time_mpo_tran=1; * mpo transit travel time;
    %let evaluate_prox=1; * proceedures to evaluate mpo proximity;
    %let use_ctpp_time=1; * merge on CTPP travel time;
* Characteristics for job accessibility (from LODES input, WHATB);
%let run_otm_attrib=1;
    * earnings, coded as earn_c, limited to low/moderate;
    %let use_otmsec=CNS01 CNS02 CNS03 CNS04 CNS05 CNS06 CNS07 CNS08 CNS09 CNS10
                    CNS11 CNS12 CNS13 CNS14 CNS15 CNS16 CNS17 CNS18 CNS19 CNS20;
    %let use_otmsec2=sc1 sc2 sc3 sc4 sc5;
    %let use_otmgrp2=gp1 gp2 gp3 gp4;
    %let use_otmvars=jobs owner naics race ethnicity earn_c; * could add industry sector here;
    * must have count equal to number of variables, naics is 20;
    %let use_otmvars_count=23;
    %let use_otmvars2=all &use_otmsec2. &use_otmgrp2. eall;
    * count of variables in above list;
    %let use_otmvars2_count=11;
    %let use_otmjob=1; * from OTM jobs;
    %let use_otmlab=1; * from OTM labor force;
* calculate accessibility;
%let run_accessibility=1; * sums up pairs by origin or home;
    %let export_access=0;
* run summary stats;
%let run_summary=1;
* run supersummary stats (Table C1);
%let run_supersummary=1;

* variables for use in mode choice models;
%let modevars=huearn_ln huearners huearnperwork_ln pikcnt 
              pop_sq_mile com_pub_rate poverty_rate owner_rate year_built_med
              age_20_24 age_35_44 age_45_54 age_55_64 
              female grp_black grp_hisp grp_other
              earn_ln ratio_tran_auto;
* choice of auto count;
%let autovars=huearners pikcnt 
              pop_sq_mile com_pub_rate poverty_rate owner_rate year_built_med
              age_20_24 age_35_44 age_45_54 age_55_64 
              female grp_black grp_hisp grp_other
              huearn_ln ratio_tran_auto;
* choice of transit, without auto;
%let tranvars_0auto=huearnperwork_ln
              pop_sq_mile com_pub_rate poverty_rate owner_rate year_built_med
              age_20_24 age_35_44 age_45_54 age_55_64 
              female grp_black grp_hisp grp_other
              earn_ln|ratio_tran_auto;
* choice of transit, with auto, dropped auto_earner auto_pik;
%let tranvars_wauto=huearnperwork_ln
              pop_sq_mile com_pub_rate poverty_rate owner_rate year_built_med
              age_20_24 age_35_44 age_45_54 age_55_64 
              female grp_black grp_hisp grp_other
              earn_ln|ratio_tran_auto;

/* macro for log and list files -------------------------------------------- */
%macro macro_loglist;
proc printto 
    log=    "&dirprogvint./loglst/macro_&macnum._&met..log" new
    print=  "&dirprogvint./loglst/macro_&macnum._&met..lst" new;
run;
%mend macro_loglist;

/* macro --------------------------------------------------------------------------- */
%macro lostjob_macro;

* include main programs;
* for: &run0cpr. &run0cen. &run0tract.=1 &run0access. &run0access2. &run0access3. &run0modechoice. &run1. &run2. &run3. &run4. &run5. &run6. &run7. &run8.;
%include "&dirprogvint./lostjob.sas";
* for: &run0access. &run0access2. &run0access3. &run0access4.;
%include "&dirprogvint./measure_access.sas";

/* macro for intermediate output files --------------------------------------------- */
    * if turned on, the program will output all of the intermediate datasets;
    * these may be large and unnecessary, but are used for QA;
%let intermed=;
%if (&run0.=1) %then %do;
%let intermed=OUTDATA.;
%end;

/* macro for setup files --------------------------------------------- */
* run through cpr once for each metro;
%if (&run0cpr.=1) %then %do; 
%let met=all;
%let macnum = 0cpr;
%macro_loglist;
%lostjob_run0cpr;
%end;
* run through census once for all metros;
%if (&run0cen.=1) %then %do; 
%let met=all;
%let macnum = 0cen;
%macro_loglist;
%lostjob_run0cen;
%end;
* run through tract characteristics once for each metro;
%if (&run0tract.=1) %then %do; 
%let met=all;
%let macnum = 0tract;
%macro_loglist;
%include "&dirprogvint./census2000.sas";
%lostjob_run0tract;
%end;
* run through accessibility once for each metro;
%if (&run0access.=1) %then %do; 
%let met=all;
%let macnum = 0access;
%macro_loglist;
* measure_access.sas;
%lostjob_run0access;
%end;
* run through supersummary of accessibility at national level;
%if (&run0access2.=1) %then %do; 
%let met=all;
%let macnum = 0access2;
%macro_loglist;
* measure_access.sas;
* US summary (right now, only set up to run supersummary for Table C1);
%us_access;
%end;
* census 2000 worker count;
%if (&run0access3.=1) %then %do; 
%let met=all;
%let macnum = 0access3;
%macro_loglist;
* measure_access.sas;
%cen2000_sf3;
%end;
* census 2000 worker count, access calc;
%if (&run0access4.=1) %then %do; 
%let met=all;
%let macnum = 0access4;
%macro_loglist;
* measure_access.sas;
%cen2000_sf3_sum;
%end;
* run through mode choice for all metros;
%if (&run0modechoice.=1) %then %do; 
%let met=all;
%let macnum = 0modechoice;
%macro_loglist;
%include "&dirprogvint./modechoice.sas";
%lostjob_run0modechoice;
%end;

* cycle through each city, choosing the appropriate state, state codes, and counties;
* in lostjob.sas;
%let metord = &metord_start.;
%do %until("%scan(&metrolist.,&metord.)"="");
    %let met=%scan(&metrolist.,&metord.);
    %let st=%scan(&stlist.,&metord.);
    %let stcode=%scan(&stcodelist.,&metord.);
    %let cty=%scan(%quote(&countylist.),&metord.,V);
    %let sep_qtr_start=%scan(&sep_qtr_start_list.,&metord.); 
    %let sep_qtr_end=%scan(&sep_qtr_end_list.,&metord.);
    %put running &met. &st. &cty.;
    * first quarter of employment data for job history: 53;
    %let emp_qtr_start=%eval(&sep_qtr_start.-&emphist_span.);
    * last quarter of employment data;
    %let emp_qtr_end=%eval(&sep_qtr_end.+&sep_qtr_span.);
    * number of quarter of employment data to draw from EHF;
    %let emp_qtrs=%eval(&emp_qtr_end.-&emp_qtr_start.+1);
    * first quarter of ecf data;
    %let ecf_qtr_start=%eval(&sep_qtr_start.-3);
    * last quarter of ecf data;
    %let ecf_qtr_end =%eval(&sep_qtr_end.+3);
    * total quarters of ecf data;
    %let ecf_qtrs =%eval(&ecf_qtr_end.-&ecf_qtr_start.+1);
                        
%if (&run1.=1) %then %do;
%let macnum = 1;
%macro_loglist;
%lostjob_run1;
%end;
%if (&run2.=1) %then %do;
%let macnum = 2;
%macro_loglist;
%lostjob_run2;
%end;
%if (&run3.=1) %then %do;
%let macnum = 3;
%macro_loglist;
%lostjob_run3;
%end;

    %let metord=%eval(&metord+1);
%end;

* national file, all metros;
%if (&run4.=1) %then %do;
%let met=all;
%let macnum = 4;
%macro_loglist;
%lostjob_run4;
%end;
%if (&run5.=1) %then %do;
%let met=all;
%let macnum = 5;
%macro_loglist;
%lostjob_run5;
%end;
%if (&run6.=1) %then %do;
%let met=all;
%let macnum = 6;
%macro_loglist;
%lostjob_run6;
%end;
%if (&run7.=1) %then %do;
%let met=all;
%let macnum = 7;
%macro_loglist;
%lostjob_run7;
%end;
%if (&run8.=1) %then %do;
%let met=all;
%let macnum = 8;
%macro_loglist;
%lostjob_run8;
%end;

%mend lostjob_macro;
* run lostjob macro;
%lostjob_macro; 

